package com.slkj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.slkj.dto.Result;
import com.slkj.dto.StatusCode;
import com.slkj.pojo.DocUserInfo;
import com.slkj.pojo.DocsReplyLog;

import com.slkj.service.DocsReplyLogService;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/${address.prefix}/doc")
public class DocsReplyLogController {

    @Autowired
    private DocsReplyLogService replyLogService;

    /**
     * 查看日志
     * @param data
     * @return
     */
    @PostMapping("/log")
    public Result replyLogByUUid(@RequestBody String data){
        log.info("查看日志UUID>>" + data);
        JSONObject jsonObject = JSON.parseObject(data);
        String docUuid = jsonObject.getString("docUuid");
        List<DocsReplyLog> byDocUUid = replyLogService.findByDocUUid(docUuid);
        return new Result(true, StatusCode.OK,"查看此公文日志",byDocUUid);
    }

    /**
     * 显示已读比例
     * @param data
     * @return
     */
    @PostMapping("/readPoint")
    public Result readPoint(@RequestBody String data){
        log.info("显示已读比例>>" + data);
        JSONObject jsonObject = JSON.parseObject(data);
        String docUuid = jsonObject.getString("docUuid");
        double point = replyLogService.readPoint(docUuid);
         String pointVO= point*100 +"%";
        log.info("显示已读比例>>" + pointVO);
        return new Result(true, StatusCode.OK,"查看已读比率",pointVO);
    }



    /**
     * 收藏文件
     */
    @PostMapping("/addCollect")
    public Result isCollect(@RequestBody String data){
        log.info("添加收藏>>" + data);
        DocUserInfo userinfo =(DocUserInfo) SecurityUtils.getSubject().getSession().getAttribute("userinfo");
        String name = userinfo.getName();
        JSONObject jsonObject = JSONObject.parseObject(data);
        String docUuid = jsonObject.getString("docUuid");
        //根据名字，和UUID确定。是谁收藏了文件。。
        replyLogService.collectLogByUUIDAndName(docUuid,name);
        return new Result(true,StatusCode.OK,"添加收藏成功");
    }
    //测试添加收藏
    /*
    @PostMapping("/addCollectTest")
    public Result testisCollect(@RequestBody String data){
        log.info("测试添加收藏>>" + data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        String docUuid = jsonObject.getString("docUuid");
        String name = jsonObject.getString("name");
        //根据名字，和UUID确定。是谁收藏了文件。。
        System.out.println("service>>" + docUuid + name);
         replyLogService.collectLogByUUIDAndName(docUuid, name);
        return new Result(true,StatusCode.OK,"测试添加收藏成功");
    }

*/









}
